using System;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class BaseStarterPackUI : MonoBehaviour
{
	public StarterPack starterPack;

	public UILabel title;

	public UILabel timer;

	public UILabel offer;

	public UILabel price;

	public UILabel oldPrice;

	public StarterPackRewardUI rewardPrefab;

	public UIGrid dafaultRewardGrid;

	public List<StarterPackRewardUI> rewardUIPanels = new List<StarterPackRewardUI>();

	public void Init(StarterPack starterPack)
	{
		this.starterPack = starterPack;
		string text = ScriptLocalization.Get("Offers/" + starterPack.eventID);
		title.text = ((!string.IsNullOrEmpty(text)) ? text : starterPack.eventID);
		offer.text = starterPack.profitPercent + "%";
		if (price != null)
		{
			price.text = GetIapPrice(starterPack.iapID);
		}
		if ((bool)oldPrice)
		{
			string text2 = InAppShop.instance.GetCurrencyCode(starterPack.iapID);
			if (string.IsNullOrEmpty(text2))
			{
				text2 = "$";
			}
			oldPrice.text = ScriptLocalization.Get("Game_Common/StarterPack_withoutSale") + Math.Ceiling(GetIapCost(starterPack) * (float)starterPack.profitPercent / 100f) + text2;
		}
	}

	public virtual void SetRewards(float scale, UIGrid container, List<StarterPackReward> rewards, bool clear = true, int maxCount = 0)
	{
		if (clear)
		{
			CleadRewards();
		}
		container.gameObject.SetActive(true);
		foreach (StarterPackReward reward in rewards)
		{
			StarterPackRewardUI starterPackRewardUI = UnityEngine.Object.Instantiate(rewardPrefab);
			starterPackRewardUI.transform.SetParent(container.transform);
			starterPackRewardUI.transform.localPosition = Vector3.zero;
			starterPackRewardUI.transform.localScale = new Vector3(scale, scale, 0f);
			starterPackRewardUI.gameObject.SetActive(true);
			starterPackRewardUI.Init(reward);
			rewardUIPanels.Add(starterPackRewardUI);
			if (maxCount > 0 && rewardUIPanels.Count >= maxCount)
			{
				break;
			}
		}
	}

	public virtual void CleadRewards()
	{
		for (int i = 0; i < rewardUIPanels.Count; i++)
		{
			UnityEngine.Object.Destroy(rewardUIPanels[i].gameObject);
		}
		rewardUIPanels.Clear();
	}

	public virtual void Update()
	{
		if (starterPack == null)
		{
			return;
		}
		if (JSONManager.Instance.ServerTimeLoad())
		{
			int num = starterPack.availableUntil - JSONManager.Instance.GetServerTime();
			if (num > 0)
			{
				timer.text = ConvertTimeToString(num);
			}
			else
			{
				timer.text = string.Empty;
			}
		}
		else
		{
			timer.text = ScriptLocalization.Get("Game_Common/lb_Connection");
		}
	}

	public virtual string ConvertTimeToString(int time)
	{
		TimeSpan timeSpan = TimeSpan.FromSeconds(time);
		if (timeSpan.Days > 0)
		{
			if (timeSpan.Days > 9)
			{
				return string.Format("{0:D2}:{1:D2}H:{2:D2}:{3:D2}", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
			}
			return string.Format("{0:D1}:{1:D2}:{2:D2}:{3:D2}", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
		}
		if (timeSpan.Hours > 0)
		{
			if (timeSpan.Hours > 9)
			{
				return string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
			}
			return string.Format("{0:D1}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
		}
		if (timeSpan.Minutes > 0)
		{
			if (timeSpan.Minutes > 9)
			{
				return string.Format("{0:D2}:{1:D2}", timeSpan.Minutes, timeSpan.Seconds);
			}
			return string.Format("{0:D1}:{1:D2}", timeSpan.Minutes, timeSpan.Seconds);
		}
		if (timeSpan.Seconds > 9)
		{
			return string.Format("{0:D2}", timeSpan.Seconds);
		}
		return string.Format("{0:D1}", timeSpan.Seconds);
	}

	public float GetIapCost(StarterPack starterPack)
	{
		float currencyPrice = InAppShop.instance.GetCurrencyPrice(starterPack.iapID);
		if (currencyPrice == 0f)
		{
			foreach (iapInfo starterPackIAPs_iO in InAppShop.instance.starterPackIAPs_iOS)
			{
				if (starterPackIAPs_iO.id == starterPack.iapID)
				{
					return starterPackIAPs_iO.cost;
				}
			}
		}
		return currencyPrice;
	}

	public string GetIapPrice(string keyInApp)
	{
		string text = InAppShop.instance.ConverctCurrency(keyInApp);
		if (string.IsNullOrEmpty(text))
		{
			text = InAppShop.instance.GetIAPCost(keyInApp) + "$";
		}
		return text;
	}

	public void ShowMainStarterPackUI()
	{
		if (starterPack != null)
		{
			MainStarterPacksUI.instance.ShowStarterPackUI(starterPack);
		}
	}
}
